package com.aqgj.common;
import com.aqgj.User.bean.User;
import com.express.ExpRelease.service.ExpReleaseManager;
import com.lv.LvTourismForSale.service.LvTourismForSaleManager;
import com.lv.LvTourismOrder.service.LvTourismOrderManager;
import com.ztc.AppUser.bean.AppUser;
import com.ztc.AppUser.service.AppUserManager;
import com.ztc.ZtcClasses.service.ZtcClassesManager;
import com.ztc.ZtcClassesForSale.service.ZtcClassesForSaleManager;
import com.ztc.ZtcClassesPriceForSale.bean.ZtcClassesPriceForSale;
import com.ztc.ZtcClassesPriceForSale.service.ZtcClassesPriceForSaleManager;
import com.ztc.ZtcDriver.bean.ZtcDriver;
import com.ztc.ZtcDriver.service.ZtcDriverManager;
import com.ztc.ZtcRoute.service.ZtcRouteManager;
import com.ztc.ZtcTransport.bean.ZtcTransport;
import com.ztc.ZtcTransport.service.ZtcTransportManager;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.text.ParseException;
import java.util.List;

/**
 * 定时任务
 * @author Jetvae
 */
@Component
@EnableScheduling
public class Quartz {

    private static final Logger log = LogManager.getLogger(Quartz.class);
    @Autowired
    private ZtcDriverManager ztcDriverManager;
    @Autowired
    private ZtcTransportManager ztcTransportManager;
    @Autowired
    private ZtcClassesManager ztcClassesManager;
    @Autowired
    private LvTourismForSaleManager lvTourismForSaleManager;
    @Autowired
    private LvTourismOrderManager lvTourismOrderManager;
    @Autowired
    private ZtcClassesForSaleManager ztcClassesForSaleManager;
    @Autowired
    private ZtcRouteManager ztcRouteManager;
    @Autowired
    private ZtcClassesPriceForSaleManager ztcClassesPriceForSaleManager;
    @Autowired
    private AppUserManager appUserManager;
    @Autowired
    private ExpReleaseManager expReleaseManager;

    /**
     * 每天凌晨2点执行（扫描营业执照已过有效期的运营公司）
     */
    @Scheduled(cron = " 0 0 2 * * ?")
    public void quartzScanZtcTransport() {
        log.info("=============》开始执行扫描营业执照已过有效期的运营公司======");
        List<ZtcTransport> ztcTransportList= ztcTransportManager.getExpireZtcTransport();
        if(ztcTransportList.size()>0){
            User user=new User();
            user.setName("系统定时任务");
            user.setId("scan0000000001");
            log.info("============》开始执行冻结公司操作======");
            for (ZtcTransport z:ztcTransportList) {
                log.info("=============》开始执行冻结‘"+z.getFullName()+"’（"+z.getId()+"）及旗下车方的操作======");
                ztcTransportManager.setTransportStatus(4,z,user);
            }
            log.info("=============》冻结公司操作已执行完成======");
        }else{
            log.info("=============》没有公司被执行冻结操作======");
        }
    }

    /**
     * 每天凌晨2点执行（扫描驾驶证证件已过有效期的顺风车司机）
     */
    @Scheduled(cron = "0 0 2 * * ?")
    public void quartzScanPrivateCar() {
        log.info("=============》开始执行扫描驾驶证证件已过有效期的顺风车司机======");
        List<AppUser> ztcAppUserList= appUserManager.getExpireAppUserList();
        if(ztcAppUserList.size()>0){
            log.info("=============》开始执行冻结顺风车司机操作======");
            for (AppUser z:ztcAppUserList) {
                log.info("=============》开始执行冻结‘"+z.getName()+"的操作======");
                z.setPrivateCarStatus(5);
                appUserManager.save(z);
            }
            log.info("=============》冻结顺风车司机操作已执行完成======");
        }else{
            log.info("=============》没有顺风车司机被执行冻结操作======");
        }
    }

    /**
     * 每天凌晨2点执行（扫描驾驶证证件已过有效期的司机）
     */
    @Scheduled(cron = "0 0 2 * * ?")
    public void quartzScanZtcDriver() {
        log.info("=============》开始执行扫描驾驶证证件已过有效期的司机======");
        List<ZtcDriver> ztcDriverList= ztcDriverManager.getExpireDriverList();
        if(ztcDriverList.size()>0){
            log.info("=============》开始执行冻结司机操作======");
            for (ZtcDriver z:ztcDriverList) {
                log.info("=============》开始执行冻结‘"+z.getdName()+"’（"+z.getId()+"）的操作======");
                z.setStatus("3");
                ztcDriverManager.update(z);
            }
            log.info("=============》冻结司机操作已执行完成======");
        }else{
            log.info("=============》没有司机被执行冻结操作======");
        }
    }
    /**
     * 每天凌晨2点执行（扫描已过期的在售旅游）
     */
    @Scheduled(cron = "0 0 2 * * ?")
    public void scanOverTourismForSale() {
        log.info("自动排班");
        String result=lvTourismForSaleManager.setOverTourismForSale();
        if("1".equals(result)){
            log.info("自动排班成功");
        }else{
            log.info("自动排班失败");
        }
    }
    /***
     * 每天凌晨0点执行（自动排班）
     * @throws ParseException
     */
    @Scheduled(cron="0 0 0 * * ?")
//    @Scheduled(cron="0/5 * * * * ?")
    public void autoArrangeClasses() throws ParseException {
        log.info("自动排班");
        String result=ztcClassesManager.autoArrangeClasses();
        if("1".equals(result)){
            log.info("自动排班成功");
        }else{
            log.info("自动排班失败");
        }
    }

    /***
     * 每天凌晨0点执行（自动排班（旅游））
     * @throws ParseException
     */
    @Scheduled(cron="0 0 0 * * ?")
    public void autoArrangeTourism() throws ParseException {
        log.info("自动排班");
        String result=lvTourismForSaleManager.autoArrangeTourism();
        if("1".equals(result)){
            log.info("自动排班成功");
        }else{
            log.info("自动排班失败");
        }
    }


    /**
     * 每隔10分钟执行（扫描已过发车时间的站点）
     * @throws ParseException
     */
    @Scheduled(cron="0 0/10 * * * ?")
    public void siteArriving() throws ParseException {
        log.info("站点到站");
        String result=ztcClassesForSaleManager.siteArriving();
        if("1".equals(result)){
            log.info("站点到站成功");
        }else{
            log.info("站点到站失败");
        }
    }

    /**
     * 每天凌晨0点执行（扫描已过有效期的线路并设置为未审核）
     */
    @Scheduled(cron="0 0 0 * * ?")
    public void examineOverdueRoute(){
        log.info("重置过期线路");
        String result=ztcRouteManager.examineOverdue();
        if("1".equals(result)){
            log.info("重置过期线路成功");
        }else{
            log.info("重置过期线路失败");
        }
    }

    /**
     * 每天凌晨0点执行（扫描在线班次发车日期已超过30天的在线班次价格）
     */
    @Scheduled(cron="0 0 0 * * ?")
//    @Scheduled(cron="0/5 * * * * ?")
    public void deleteOverduePriceForSale(){
        log.info("删除过期在售班次上价格及座位");
        List<ZtcClassesPriceForSale> list=ztcClassesPriceForSaleManager.searchOverduePriceForSale();
        int deleteCount=0;
        for(int i=0;i<list.size();i++){
            ZtcClassesPriceForSale price=list.get(i);
            String result=ztcClassesPriceForSaleManager.delete(price.getId(),null,null);
            if("0".equals(result)){
                log.info("删除过期在售班次上价格及座位失败");
            }
            deleteCount++;
        }
        if(deleteCount==list.size()){
            log.info("删除过期在售班次上价格及座位成功");
        }else{
            log.info("删除过期在售班次上价格及座位失败");
        }
    }
    /**
     * 扫描过期订单（每1分钟扫描一次）.
     */
    @Scheduled(cron = "0 0/1 * * * ?")
    public void scanTourismOrder(){
        lvTourismOrderManager.setOrderCancelStatus();
    }
    /**
     * 扫描快递比例退款订单完成资金到账（每1分钟扫描一次）.
     * 条件：
     * 1.比例退款大于7天，且待处理的订单
     */
    @Scheduled(cron = "0 0/1 * * * ?")
    public void scanExpRefundOrder(){
        expReleaseManager.scanExpRefundOrder();
    }
}
